package com.boe.common.util.freemark.base;

import lombok.*;
import com.boe.common.util.ClassUtil;
import com.boe.common.util.freemark.P0Error;
import com.boe.common.util.freemark.BaseMethodModelFreemarker;
import freemarker.template.TemplateModelException;

import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;


public class FreemarkerIsType extends BaseMethodModelFreemarker {
    @Override
    public Object exec(List list) throws TemplateModelException {
        var paramValue = getFreemarkerParameter(list.get(0));
        List<String> pp = (List<String>) list.stream().skip(1).map(it -> it.toString()).collect(Collectors.toList());

        var clazzes = getFreemarkerParameterList(pp.toArray(new String[pp.size()]));
        if (paramValue instanceof Field) {
            return clazzes.stream().anyMatch(it -> ClassUtil.isType(((Field) paramValue).getType(), it));

        } else if (paramValue instanceof Class) {
            return clazzes.stream().anyMatch(it -> ClassUtil.isType((Class) paramValue, it));
        }
        throw new P0Error(paramValue);
    }
}
